<template>
  <el-select :value="valueId" ref="el_select">
    <!-- <el-input class="selectInput" :placeholder="placeholder" v-model="filterText"></el-input> -->
    <el-option :value="valueId" :label="valueTitle" class="options">
      <el-tree id="tree-option" ref="selectTree" :expand-on-click-node="false" :default-expand-all="true" :data="orgTableData" :props="props" :node-key="props.value" :default-expanded-keys="defaultExpandedKey" :filter-node-method="filterNode" @node-click="handleNodeClick">
      </el-tree>
    </el-option>
  </el-select>
</template>
  
<script>
import { getOrgTree } from "@/api/organization";
export default {
  name: "el-tree-select",
  props: {
    /* 配置项 */
    props: {
      type: Object,
      default: () => {
        return {
          value: 'id',             // ID字段名
          label: 'orgName',         // 显示名称
          children: 'children'    // 子级字段名
        }
      }
    },
    /* 初始值 */
    value: {
      type: String,
      default: () => { return null }
    },
    /* 初始值 */
    companyId: {
      type: String,
      require: true,
    }
  },
  data () {
    return {
      filterText: '',
      valueId: this.value,    // 初始值
      valueTitle: '',
      defaultExpandedKey: [],
      orgTableData: []
    }
  },
  mounted () {
    // 
    this.getOrgTree()
  },
  methods: {
    // 初始化值
    initHandle () {
      if (this.valueId) {
        this.valueTitle = this.$refs.selectTree.getNode(this.valueId).data[this.props.label]     // 初始化显示
        this.$refs.selectTree.setCurrentKey(this.valueId)       // 设置默认选中
        this.defaultExpandedKey = [this.valueId]      // 设置默认展开
      }
      this.initScroll()
    },
    // 初始化滚动条
    initScroll () {
      this.$nextTick(() => {
        let scrollWrap = document.querySelectorAll('.el-scrollbar .el-select-dropdown__wrap')[0]
        let scrollBar = document.querySelectorAll('.el-scrollbar .el-scrollbar__bar')
        scrollWrap.style.cssText = 'margin: 0px; max-height: none; overflow: hidden;'
        scrollBar.forEach(ele => ele.style.width = 0)
      })
    },
    // 切换选项
    handleNodeClick (node) {
      this.valueTitle = node[this.props.label]
      this.valueId = node[this.props.value]
      this.$emit('setOrgData', this.valueId)
      this.defaultExpandedKey = []
      this.$refs.el_select.blur() //手动关闭选择框
    },
    // getSelectedNodes () {
    //   const selectedItems = this.$refs.selectTree.getCheckedNodes(true);
    //   this.$emit("setOrgData", selectedItems);
    // },
    filterNode (value, data) {
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    },
    getOrgTree () {
      const param = { companyId: this.companyId }
      getOrgTree(param).then((res) => {
        if (res.status === 200) {
          this.orgTableData = res.data;
          const that = this
          setTimeout(function () {
            that.initHandle()
          }, 100);
        } else {
          this.$message(res.message);
        }
      });
    },
  },
  watch: {
    companyId (newValue, oldValue) {
      // 在这里可以执行更多的逻辑
      if (newValue.length > 0 || newValue !== undefined) {
        this.getOrgTree()
      }
    }
  },
};
</script>
  
  <!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.el-scrollbar .el-scrollbar__view .el-select-dropdown__item {
  height: auto;
  max-height: 274px;
  padding: 0;
  overflow: hidden;
  overflow-y: auto;
}
.el-select-dropdown__item.selected {
  font-weight: normal;
}
ul li >>> .el-tree .el-tree-node__content {
  height: auto;
  padding: 0 20px;
}
.el-tree-node__label {
  font-weight: normal;
}
.el-tree >>> .is-current .el-tree-node__label {
  color: #409eff;
  font-weight: 700;
}
.el-tree >>> .is-current .el-tree-node__children .el-tree-node__label {
  color: #606266;
  font-weight: normal;
}
.selectInput {
  padding: 0 5px;
  box-sizing: border-box;
}
</style>
  
  
  